package com.demo.service.impl;


import java.util.List;

import com.demo.entity.po.DeviceInfo;

import com.demo.entity.po.DeviceInventory;
import com.demo.entity.query.DeviceInfoQuery;

import com.demo.entity.query.DeviceInventoryQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.DeviceInventoryMapper;
import com.demo.service.DeviceInfoService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.DeviceInfoMapper;

import com.demo.entity.enums.PageSize;

/**
 * @Description:serviceImpl
 * @Date:2025-03-11
 * @author：author
*/
@Service("deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService{

	@Resource
	private DeviceInfoMapper<DeviceInfo,DeviceInfoQuery> deviceInfoMapper;

	@Resource
	private DeviceInventoryMapper<DeviceInventory, DeviceInventoryQuery> deviceInventoryMapper;

	/**
	 * 根据条件查询列表
	 */
	public List<DeviceInfo> findListByParam(DeviceInfoQuery query) { 
		return this.deviceInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(DeviceInfoQuery query){
		return this.deviceInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<DeviceInfo> findListByPage(DeviceInfoQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<DeviceInfo> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(DeviceInfo bean) { 
		return this.deviceInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<DeviceInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<DeviceInfo> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.deviceInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据DeviceId查询
	 */
	public DeviceInfo getDeviceInfoByDeviceId(Integer deviceId) { 
		return this.deviceInfoMapper.selectByDeviceId(deviceId);
	}

	/**
	 * 根据DeviceId更新
	 */
	public Integer updateDeviceInfoByDeviceId(DeviceInfo bean, Integer deviceId) { 
		return this.deviceInfoMapper.updateByDeviceId(bean,deviceId);
	}



	/**
	 * 根据DeviceId删除
	 */
	public Integer deleteDeviceInfoByDeviceId(Integer deviceId) {
		DeviceInfo deviceInfo = deviceInfoMapper.selectByDeviceId(deviceId);
		if (deviceInfo == null) {
			throw new BusinessException("实验器材已经删除，或不存在");
		}
		DeviceInventoryQuery inventoryQuery = new DeviceInventoryQuery();
		inventoryQuery.setEquipmentId(deviceId);
		Integer count = deviceInventoryMapper.selectCount(inventoryQuery);
		if (count>0){
			throw new BusinessException("该实验器材有库存信息，请先清理库存信息");
		}
		return this.deviceInfoMapper.deleteByDeviceId(deviceId);
	}

	/**
	 * 保存设备基础信息
	 * @param bean
	 */
	@Override
	public void saveDeviceInfo(DeviceInfo bean) {
		if (bean.getDeviceId() == null) {
			deviceInfoMapper.insert(bean);
		}else {
			deviceInfoMapper.updateByDeviceId(bean,bean.getDeviceId());
		}
	}
}